package com.seeyon.apps.attendance.data;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Author: SPK
 * @Date: 2021/9/26 13:37
 * @Description:
 */
public class DingdingOrgData {
    private static final Log log = LogFactory.getLog(DingdingOrgData.class);
    private static ResourceBundle bundle = ResourceBundle.getBundle("HzwDing");
    private static String appKey = bundle.getString("appKey");
    private static String appSecret = bundle.getString("appSecret");
    private String token;

    /**
     * 获取日期
     * @return
     */

    /**
     * 获取token
     *
     * @return
     */
    public void getToken() {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest req = new OapiGettokenRequest();
        req.setAppkey(appKey);
        req.setAppsecret(appSecret);
        req.setHttpMethod("GET");
        OapiGettokenResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            log.error("获取token失败", e);
        }
        this.token = rsp.getAccessToken();
    }

    /**
     * 获取部门列表
     *
     * @return
     */
    public List<OapiDepartmentListResponse.Department> getDepartmentList() {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
        OapiDepartmentListRequest req = new OapiDepartmentListRequest();
        req.setHttpMethod("GET");
        OapiDepartmentListResponse rsp = null;
        try {
            rsp = client.execute(req, token);
        } catch (ApiException e) {
            log.error("获取部门列表失败", e);
        }
        List<OapiDepartmentListResponse.Department> departmentList = rsp.getDepartment();
        return departmentList;
    }

    /**
     * 获取人员id列表
     *
     * @param deptId
     * @param cursor
     * @return
     */
    public OapiV2UserListResponse getUserList(Long deptId, Long cursor) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
        OapiV2UserListRequest req = new OapiV2UserListRequest();
        req.setDeptId(deptId);
        req.setCursor(cursor);
        req.setSize(30L);
        OapiV2UserListResponse rsp = null;
        try {
            rsp = client.execute(req, token);
        } catch (ApiException e) {
            log.error("获取人员id列表失败", e);
        }
        return rsp;
    }


   public void getAttendance(String workDateFrom, String workDateTo, ArrayList<String> userIds, Long offset,ArrayList<OapiAttendanceListResponse.Recordresult> attendances){
       DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/attendance/list");
       OapiAttendanceListRequest req = new OapiAttendanceListRequest();
       OapiAttendanceListResponse rsp = null;
       try {
           req.setWorkDateFrom(workDateFrom);
           req.setWorkDateTo(workDateTo);
           req.setUserIdList(userIds);
           req.setOffset(offset);
           req.setLimit(50L);
           rsp = client.execute(req, token);
       } catch (ApiException e) {
           log.error("获取考勤数据失败", e);
       }
       List<OapiAttendanceListResponse.Recordresult> recordresult = rsp.getRecordresult();
       for (int i = 0; i < recordresult.size(); i++) {
           attendances.add(recordresult.get(i));
       }
       Boolean hasMore = rsp.getHasMore();
       if(hasMore){
           getAttendance(workDateFrom,workDateTo,userIds,offset+50,attendances);
       }
   }
}